package org.github.xhjcehust.dubbo.proxy.util;

import cn.htd.secretcenter.service.SecretForDesService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

@Component
public class SecretUtil {

    private final String [] SECRET_ARR = {
            "artificialPersonMobile","taxManId","artificialPersonIdcard","contactMobile",
            "locationDetail","locationAddr","actualBusinessDetail","actualBusinessAddress",
            "businessLicenseId","buyerBusinessLicenseId","businessPersonMobile","operatingMobile",
            "operatingIdcard"};

    private final List<String> SECRET_FIELD = Arrays.asList(SECRET_ARR);

    private final int MAX_LIMIT = 50;

    @Reference(version="${dubbo.registry.version}",registry = "${dubbo.registry.address}")
    private SecretForDesService secretForDesService;


    public boolean decryptList(List<?> list) {
        boolean isSuccess = true;
        try{
            List<List<String>> secretStrList = new ArrayList<>();
            List<Map<String,String>> secretMapList = new ArrayList<>();
            for(Object obj : list){
                Field[] fields = obj.getClass().getDeclaredFields();
                List<String> secretStr = new ArrayList<>();
                Map<String,String> secretMap = new HashMap<>();
                for(Field field:fields){
                    field.setAccessible(true);
                    if(SECRET_FIELD.contains(field.getName())&&field.get(obj)!=null){
                        secretStr.add(field.get(obj).toString());
                        secretMap.put(field.getName(),field.get(obj).toString());
                    }
                }
                secretStrList.add(secretStr);
                secretMapList.add(secretMap);
            }

            //调用解密服务
            List<Map<String, String>> secretList = new ArrayList<>();
            for(int i=0,l=secretStrList.size()/MAX_LIMIT+1;i<l;i++){

                int fromIndex = i*MAX_LIMIT;
                int toIndex = secretStrList.size()-(i+1)*MAX_LIMIT;

                List<List<String>> tmpList = secretStrList.subList(fromIndex,toIndex<=0?secretStrList.size():secretStrList.size()-toIndex);
                if(tmpList.size()>0){
                    secretList.addAll(secretForDesService.decryptList(tmpList,"",""));
                }
            }
            if(secretMapList.size()!=secretList.size()){
                throw new RuntimeException("解密服务异常，长度不一致");
            }

            //组装返回值
            for(int i=0,l=secretList.size();i<l;i++){

                Map<String, String> resultMap = secretList.get(i);
                Map<String,String> map = secretMapList.get(i);
                Object dto = list.get(i);

                for(Map.Entry<String, String> entry : map.entrySet()){
                    String k = entry.getKey();
                    String v = resultMap.get(entry.getValue());

                    Field field = dto.getClass().getDeclaredField(k);
                    if(field!=null){
                        field.setAccessible(true);
                        field.set(dto,v==null?entry.getValue():v);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            isSuccess = false;
        }
        return isSuccess;
    }
}
